package com.woniu.p2p.behindServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.UserRepayService;
import com.woniu.p2p.entity.Loans;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserMonloans;
import com.woniu.p2p.mapper.LoansMapper;
import com.woniu.p2p.mapper.UserMapper;
import com.woniu.p2p.mapper.UserMonloansMapper;
import com.woniu.p2p.vo.UserRepayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserRepayServiceImpl implements UserRepayService {

    @Autowired
    UserMonloansMapper userMonloansMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    LoansMapper loansMapper;

    @Override
    public PageInfo<UserRepayVo> findAllUserRepayForPageInfo(Integer start, Integer num, String repaystatus, Date predate, Date nextdate, String repaykeyword) {
        PageHelper.startPage(start,num);
        List<UserRepayVo> list = userMonloansMapper.findAllUserRepayForPage(repaystatus, repaykeyword, predate, nextdate);
        PageInfo<UserRepayVo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public User findAllUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public Loans findAllLoansById(Integer id) {
        return loansMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteUserRepay(Integer id) {
        UserMonloans userMonloans = userMonloansMapper.selectByPrimaryKey(id);
        Long monloansPaybefore = userMonloans.getMonloansPaybefore();
        if(monloansPaybefore!=null){
            return 0;
        }else {
            userMonloansMapper.deleteByPrimaryKey(id);
            return 1;
        }
    }

    @Override
    public List<UserRepayVo> findAllMonloans(int[] id) {
        return userMonloansMapper.findAllMonloasnByIdes(id);
    }
}
